package hhm.android.main

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import hhm.android.base.fragment.NewLazyFragment

import hhm.android.library.utils.SpacesItemDecoration
import siau.android.base.HttpHelper
import siau.android.base.SBApplication
import siau.android.http.model.DeviceModel

class ThreeFragment(var statusBarHeightPx: Int) : NewLazyFragment() {

    constructor() : this(20)

    lateinit var recycler: RecyclerView
    lateinit var adapter: MachineAdapter
    lateinit var topView: View

    override fun getContentViewId(): Int {
        return R.layout.fragment_three
    }

    override fun initView(view: View?) {
        super.initView(view)
        recycler = view?.findViewById(R.id.fragment_three_recycler)!!

        val list = ArrayList<DeviceModel>()

        recycler.layoutManager = LinearLayoutManager(activity)
        recycler.addItemDecoration(SpacesItemDecoration(dp2px(16f)))
        adapter = activity?.let { MachineAdapter(it, list) }!!
        recycler.adapter = adapter

        topView = view.findViewById(R.id.fragment_three_top_view)
        val layoutParams = topView.layoutParams
        layoutParams.height = statusBarHeightPx
        topView.layoutParams = layoutParams
    }

    override fun getData() {
        super.getData()
        HttpHelper().getBindDeviceList()?.subscribe({
            SBApplication.userData.deviceList = it
            adapter.setData(it)
        }, {
            showToast(it.message)
        })
    }
}